package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.PageList;
import cn.itsource.pethome.Basic.util.SystemContext;
import cn.itsource.pethome.Basic.util.UserContext;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.query.SearchMasterPendingQuery;
import cn.itsource.pethome.pet.service.ISearchMasterService;
import cn.itsource.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMaster")
public class SearchMasterMessageController {
    @Autowired
    private ISearchMasterService searchMasterService;
    @Autowired
    private IShopService shopService;
    /**
     * 寻主
     * @param searchMaster
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMessage searchMaster,HttpServletRequest request){
        try {
            //获取用户
            User loginUser = UserContext.getLoginUser(request);
            searchMaster.setUser(loginUser);
            searchMasterService.publish(searchMaster);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 通过条件查询寻主信息
     * @param query
     * @return
     */
    @PatchMapping("/searchMasterByQuery")
    public PageList<SearchMasterMessage> handle(@RequestBody SearchMasterPendingQuery query){
        return searchMasterService.loadAllByQuery(query);
    }

    /**
     * 处理宠物信息
     * @param pet
     * @return
     */
    @PostMapping("/handlePendingMessage")
    public AjaxResult handlePendingMessage(@RequestBody Pet pet){
        try {
            //获取当前商家登录用户
            Employee loginUser = SystemContext.getLogin();
            //通过登录用户获取店铺
            Shop shop = shopService.findByEmployee(loginUser);
            pet.setShop(shop);
            searchMasterService.handlePendingMessage(pet);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
